<template>
  <div id="personal">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>单位通讯录</el-breadcrumb-item>
      <el-breadcrumb-item>单位通讯录</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row type="flex" class="row-bg">
      <el-col :span="4">
        <div class="grid-content bg-purple">
          <div id="list">
            单位通讯录
            <p id="all" @click="all">全部</p>
            <el-tree :data="treeDataTrans" accordion :default-expand-all="true" :props="defaultProps" @node-click="handleNodeClick"></el-tree>

          </div>
        </div>
      </el-col>
      <el-col :span="20">
        <div class="grid-content bg-purple-light">
          <div id="listbox">
            <el-row>
              <el-button type="primary">新增</el-button>
              <el-button type="primary" @click="ceshi">删除</el-button>
              <!-- <el-button type="primary">修改</el-button>
              <el-button type="primary">短信</el-button> -->
              <SearchInput />
            </el-row>
          </div>
        </div>

        <!-- <Letterlist /> -->
        <div>
          <el-table ref="multipleTable" :data="corporate" tooltip-effect="dark" style="width: 100%">
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column prop="corporate_name" label="姓名" width="100">
            </el-table-column>
            <el-table-column prop="email" label="电子邮箱" width="200">
            </el-table-column>
            <el-table-column prop="tele" label="手机号码" width="160">
            </el-table-column>
            <el-table-column prop="phone" label="办公电话" width="160">
            </el-table-column>
            <el-table-column prop="department" label="部门">
            </el-table-column>

            <el-table-column prop="company" label="单位">
            </el-table-column>
            <el-table-column prop="position" label="职位">
            </el-table-column>

            <el-table-column prop="office" label="办公室" show-overflow-tooltip>
            </el-table-column>
          </el-table>
        </div>

        <div>
          <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="this.total">
            </el-pagination>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import Letterlist from "../components/Letterlist.vue";
import SearchInput from "../components/SearchInput.vue";
export default {
  components: {
    Letterlist,
    SearchInput,
  },


  data() {
    return {
      currentPage: 1,
      count: 5,
      corporate: [],
      total: 0,
      treeData: [],
      treeDataTrans: [],

      defaultProps: {
        children: "children",
        label: "label",
      },
      checkCompany: '',
      checkGroup: ''
    };
  },

  methods: {
    handleNodeClick(data, node) {
      const parent = node.parent
      if (parent.data.label) {
        this.checkGroup = data.label;
        this.checkCompany = parent.data.label
      } else {
        this.checkCompany = data.label;
        this.checkGroup = ""
      }

    },
    handleSizeChange(val) {
      this.count = val
      this.getCorporate()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getCorporate()
    },
    //获取单位通讯录信息

    getCorporate() {

      this.$axios({
        url: "/addressbook/corporate/get",
        params: {
          count: this.count,
          page: this.currentPage,
          input: this.input,
          checkGroup: this.checkGroup,
          checkCompany: this.checkCompany
        },

      }).then((res) => {

        this.corporate = res.data.searchData;
        this.total = res.data.total
        this.treeData = res.data.data
      });
    },
    ceshi() {
      // console.log(this.treeData, this.corporate);
      console.log(this.treeData);

    },
    // 将单位及所属部门的数据里面key值相同的合并到新的数组
    merge(arr) {
      let tempArr = [];
      let endData = [];
      for (let i = 0; i < arr.length; i++) {
        if (tempArr.indexOf(arr[i].company) === -1) {


          endData.push({
            label: arr[i].company,
            children: [{ label: arr[i].department }],
          });
          tempArr.push(arr[i].company);
        } else {
          for (let j = 0; j < endData.length; j++) {
            if (endData[j].label == arr[i].company) {
              endData[j].children.push({ label: arr[i].department });
              break;
            }
          }
        }
      }
      return (this.treeDataTrans = endData); // 最终输出

    },
    // 点击全部
    all: function () {
      this.checkCompany = "";
      this.checkGroup = ""
      this.getCorporate()
    }


  },

  created: function () {
    this.getCorporate()
    this.merge(this.treeData)

  },


  computed: {
    input: function () {
      return this.$store.state.Addressbook.input2;
    },
  },
  watch: {
    input: function () {
      this.currentPage1 = 1;
      this.getCorporate()
    },
    treeData: function () {
      this.merge(this.treeData)
    },
    checkGroup: function () {
      this.getCorporate()
    }
  },
};
</script>

<style scoped>
#personal {
  height: 100%;
  text-align: left;
  line-height: 16px;
}
.el-breadcrumb {
  height: 7%;
}
.row-bg {
  background-color: white;
  height: 93%;
}

.el-col-4 {
  border: 1px solid #f2f2f2;
}
.el-col-20 {
  text-align: left;
}
#list,
#listbox,
#select {
  margin: 20px 0 0 20px;
  font-size: 16px;
  line-height: 16px;

  text-align: left;
}

.el-menu-vertical-demo {
  margin-top: 10px;
}
.el-tree,
#all {
  margin-top: 20px;
  cursor: pointer;
}
.block {
  position: absolute;
  right: 0;
  margin-top: 10px;
}
</style>